package com.industry.system.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.industry.framework.common.Result;
import com.industry.system.controller.vo.UserInfoVo;
import com.industry.system.convert.SystemUserConvert;
import com.industry.system.dal.dataobject.SystemMenu;
import com.industry.system.utils.SystemMenuUtils;
import com.industry.system.dal.dataobject.SystemUser;
import com.industry.system.service.AuthService;
import com.industry.system.service.SystemMenuService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Resource
    private AuthService authService;
    @Resource
    private SystemMenuService systemMenuService;

    /**
     * @description: 登录
     * @author: jsy
     * @time: 2025/1/6 16:10
     */
    // 测试登录  ---- http://localhost:8080/webapi/auth/doLogin?userName=jsy&pwd=123456
    @GetMapping("do-login")
    public Result<SaTokenInfo> doLogin(@RequestParam("userName") String userName, @RequestParam("pwd") String pwd) {
        SaTokenInfo saTokenInfo = authService.doLogin(userName, pwd);
        if (saTokenInfo != null) {
            return Result.success(saTokenInfo);
        }
        return Result.fail("用户名或密码错误");
    }

    /**
     * @description: 查询登录状态
     * @author: jsy
     * @time: 2025/1/6 16:13
     */
    // 查询登录状态  ---- http://localhost:8080/webapi/auth/isLogin
    @PostMapping("is-login")
    public Result<Boolean> isLogin() {
        return Result.success(StpUtil.isLogin());
    }

    /**
     * @description: 查询 Token 信息
     * @author: jsy
     * @time: 2025/1/6 16:11
     */
    // 查询 Token 信息  ---- http://localhost:8080/webapi/auth/tokenInfo
    @GetMapping("token-info")
    public Result<SaTokenInfo> tokenInfo() {
        return Result.success(StpUtil.getTokenInfo());
    }

    /**
     * @description: 注销
     * @author: jsy
     * @time: 2025/1/6 16:10
     */
    // 测试注销  ---- http://localhost:8080/webapi/auth/logout
    @PostMapping("logout")
    public Result logout() {
        StpUtil.logout();
        return Result.success();
    }

    /**
     * @description: 获取用户信息
     * @author: jsy
     * @time: 2025/1/6 16:20
     */
    // 测试获取用户信息---- http://localhost:8080/webapi/auth/getUserInfo
    @GetMapping("get-user-info")
    public Result<UserInfoVo> getUserInfo() {
        SystemUser systemUser = authService.getUserInfo();
        if (systemUser == null) {
            return Result.fail("获取用户信息失败");
        }
        UserInfoVo vo = new UserInfoVo();
        vo.setSystemUserRespVO(SystemUserConvert.INSTANCE.convert(systemUser));
        List<SystemMenu> userMenuInfosByLogin = systemMenuService.getUserMenuInfosByLogin();
        vo.setModuleRespVOList(SystemMenuUtils.moduleInfoBuilder(userMenuInfosByLogin));
        vo.setMenuTreeRespVOList(SystemMenuUtils.menuTreeBuilder(userMenuInfosByLogin));
        return Result.success(vo);
    }

    /**
     * @description: 提供密码进行二级认证
     * @author: jsy
     * @time: 2025/1/10 14:18
     */
    @GetMapping("open-safe")
    public Result<String> openSafe(@RequestParam("pwd") String pwd) {
        // 比对密码
        if (authService.openSafe(pwd)) {
            // 比对成功，为当前会话打开二级认证，有效期为120秒
            StpUtil.openSafe(120);
            return Result.success("二级认证成功");
        }
        // 如果密码校验失败，则二级认证也会失败
        return Result.fail("二级认证失败");
    }
}
